package com.hhl.lamda.foreach;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HHL
 * @description
 * @date 2018/11/17 - 13:55
 */
public class ForEach {
    public static void main(String[] args) {

        Map<String, Integer> items = new HashMap<>();

        items.put("A", 10);
        items.put("B", 20);
        items.put("C", 30);
        items.put("D", 40);
        items.put("E", 50);
        items.put("F", 60);

        for (Map.Entry<String, Integer> entry : items.entrySet()) {
            System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue());
        }


        System.out.println();
        System.out.println();
        System.out.println();
        /* Lamda 表达式的 forEach循环遍历Map */
        items.forEach((k,v) -> System.out.println("键==》" + k + "  值===》" + v));





        System.out.println();
        System.out.println();
        System.out.println();
        /* Lamda 表达式的 forEach循环遍历Map */
        items.forEach((k, v) -> {
            System.out.println("键==》" + k + "  值===》" + v);
            if (k.equals("A")) {
                System.out.println("Hello A");
            }
        });




        System.out.println();
        System.out.println();
        System.out.println();
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");

        //Output : A,B,C,D,E
        for(String item : list){
            System.out.print(item + "   ");
        }





        System.out.println();
        System.out.println();
        System.out.println();
        /* Lamda 表达式的 forEach循环遍历List */
        //lambda
        //Output : A
        list.forEach(item -> {
            if (item.equals("A")) {
                System.out.println(item);
            }
        });





        System.out.println();
        System.out.println();
        System.out.println();
        /* Lamda 表达式的 forEach循环遍历List */
        //method reference
        //Output : A,B,C,D,E
        list.forEach(System.out::print);
        System.out.println();





        System.out.println();
        System.out.println();
        System.out.println();
        /* Lamda 表达式的 forEach循环遍历List */
        //Stream and filter
        //Output : A
        list.stream()
                .filter(s -> s.contains("A"))
                .forEach(System.out::print);
        System.out.println();

    }
}
